import { Injectable } from '@angular/core';
import { RestService, PagedResultDto } from '@abp/ng.core';
import { Observable } from 'rxjs';

// DTO 和 Filter 定义
export interface CustomerGood {
  id: string;
  creatorName: string;
  creationTime: string;
  purchaseOrderNo: string;
  supplierCode: string;
  estimatedArrivalTime?: string;
  customerOrderNo: string;
  carNo?: string;
  driverName?: string;
  driverPhone?: string;
  remarks?: string;
  status: string;
  shipper?: string;
  transportNo?: string;
  transportCompany?: string;
  supplier?: string;
  items: GoodCodeCreateDto[];
}

// 搜索过滤条件
export interface CustomerGoodFilter {
  purchaseOrderNo?: string;
  expectedArrivalDateFrom?: string;
  expectedArrivalDateTo?: string;
  customerOrderNo?: string;
  carNo?: string;
  driverName?: string;
  driverPhone?: string;
  status?: string;
}

/**
 * 创建进货通知单的明细 DTO
 */
export interface GoodCodeCreateDto {
  productCode: string;
  productName?: string; // 为UI回填新增可选的商品名字段
  sku?: string;
  barcode?: string;
  specification?: string;
  brand?: string;
  quantity: number | null;
  plannedStorageSlots: number | null;
  status: 'Initial' | 'Yes' | 'No';
}

/**
 * 创建进货通知单的主体 DTO
 */
export interface CustomerGoodCreateDto {
  purchaseOrderNo: string;
  supplierCode?: string;
  carNo?: string;
  driverName?: string;
  driverPhone?: string;
  status: string;
  shipper?: string;
  estimatedArrivalTime?: string;
  customerOrderNo: string;
  transportNo?: string;
  transportCompany?: string;
  supplier?: string;
  reservedWarehouseArea?: number;
  warehouse?: number;
  orderType?: number;
  remarks?: string;
  attachmentPath?: string;
  // 假设关联的明细在这里
  items: GoodCodeCreateDto[];
}

export interface GoodsModel {
  ownerCode: string;
  productCode: string;
  sku: string;
  productName: string;
  unit: string;
  temperatureZone: string;
  barcode: string;
  specification: string;
  brand: string;
}

@Injectable({ providedIn: 'root' })
export class CustomerGoodService {
  // 注意：API 路径要和后端 AppService 的名字匹配
  apiName = 'app';

  constructor(private restService: RestService) {}

  // 调用后端 GetListAsync - 用于获取全部数据
  getList(
    input: { skipCount: number; maxResultCount: number; sorting?: string }
  ): Observable<PagedResultDto<CustomerGood>> {
    return this.restService.request<void, PagedResultDto<CustomerGood>>({
      method: 'GET',
      url: '/api/app/customergoods',
      params: input,
    });
  }

  // 调用后端 GetListByConditionsAsync - 用于按条件查询
  getListByConditions(
    input: CustomerGoodFilter & { skipCount: number; maxResultCount: number; sorting?: string }
  ): Observable<PagedResultDto<CustomerGood>> {
    return this.restService.request<any, PagedResultDto<CustomerGood>>({
      method: 'POST',
      url: '/api/app/customergoods/by-conditions',
      body: input,
    });
  }

  create(input: CustomerGoodCreateDto): Observable<CustomerGood> {
    return this.restService.request<CustomerGoodCreateDto, CustomerGood>({
      method: 'POST',
      url: '/api/app/customergoods',
      body: input,
    });
  }

  getById(id: string): Observable<CustomerGood> {
    return this.restService.request<void, CustomerGood>({
      method: 'GET',
      url: `/api/app/customergoods/${id}`,
    });
  }

  // 其他方法，如 get, create, update, delete
}

@Injectable({ providedIn: 'root' })
export class GoodsService {
  apiName = 'app';

  constructor(private restService: RestService) {}

  getList(input: any): Observable<PagedResultDto<GoodsModel>> {
    return this.restService.request<any, PagedResultDto<GoodsModel>>({
      method: 'GET',
      url: '/api/app/goods',
      params: input,
    });
  }
}